package com.xintonghua.bussiness.ui.view;


import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xintonghua.bussiness.R;

/**
 * 产品计数器
 *
 * @author lrf
 */
public class GoodsCounter extends LinearLayout implements TextWatcher {
    // 控件中关键组件
    /**
     * 计数器的id
     */
    private int goodsCounterId;
    /**
     * 购买数量减一按钮
     */
    private ImageView mDecrease;
    /**
     * 购买数量加一按钮
     */
    private ImageView mIncrease;
    /**
     * 当前的购买数量编辑
     */
    private EditText mInput;

    private int mTotalNum;
    private boolean isAdd;
    /**
     * 购买数量改变监听
     */
    private OnBuyNumChangedListener mListener;
    /**
     * 最大数量 默认-1；无上限
     */
    private int max = -1;
    /**
     * 最小数量 默认为0
     */
    private int min = 0;
    /**
     * 默认为1
     */
    private int Thedefault = 1;
    Context context;

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getThedefault() {
        return Thedefault;
    }

    public void setThedefault(int thedefault) {
        Thedefault = thedefault;
        mInput.setText("" + Thedefault);
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle 在xml创建但是并且指定style的时候被调用
     */
    public GoodsCounter(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        View localView = LayoutInflater.from(context).inflate(R.layout.num_count_layout, this, true);// inflate方法直接将视图添加到父组件中
        mDecrease = (ImageView) localView.findViewById(R.id.decrease_goods_num);
        mIncrease = (ImageView) localView.findViewById(R.id.increase_goods_num);
        mInput = (EditText) localView.findViewById(R.id.goods_num);
        mInput.setText("" + Thedefault);
        mInput.setSelection(mInput.getText().toString().trim().length());
        mInput.addTextChangedListener(this);

        initClistener();
    }

    /**
     * @param context
     * @param attrs   在xml创建但是没有指定style的时候被调用
     */
    public GoodsCounter(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init(context);
    }

    public void setOnNumberChangerClistener(OnBuyNumChangedListener onBuyNumChangedListener) {
        this.mListener = onBuyNumChangedListener;
    }

    private void initClistener() {
        // TODO Auto-generated method stub
        mDecrease.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (initNumber(context, mInput, 0, min, max)) {
                    if (mListener != null) {
                        mListener.onBuyNumChanged(mInput.getText().toString().trim());
                    }
                }
            }
        });

        mIncrease.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (initNumber(context, mInput, 1, min, max)) {
                    if (mListener != null) {
                        mListener.onBuyNumChanged(mInput.getText().toString().trim());
                    }
                }

            }
        });
    }

    /**
     * @param context
     * @param number
     * @param add_or_sub 0表示-1,1表示+1
     * @param min        最小
     * @param max        最大 如果为-1 就不限定
     */
    public boolean initNumber(Context context, EditText number, int add_or_sub, int min, int max) {
        // TODO Auto-generated method stub
        int num = 0;
        try {
            num = Integer.parseInt(number.getText().toString().trim());
        } catch (Exception e) {
            // TODO: handle exception
            num = 0;
        }

        if (add_or_sub == 0) {
            if (num <= min) {

                return false;
            } else {
                number.setText("" + (num - 1));
                return true;
            }
        } else if (add_or_sub == 1) {
            if (max != -1) {
                if (num >= max) {
                    return false;
                } else {
                    number.setText("" + (num + 1));
                    return true;
                }
            } else {
                number.setText("" + (num + 1));
                return true;
            }

        }
        return false;

    }

    /**
     * @param number
     */
    public void setNumber(int number) {
        mInput.setText("" + number);
    }

    public String getNumber() {
        return mInput.getText().toString().trim();
    }

    //
    // /**
    // * 从xml文件找到该组件必须调用这个方法启动该组件
    // *
    // * @param buyNumChangedListener
    // * 购买数量改变监听
    // * @param mTotalNum
    // * 当前购买数不能小于1
    // * @param userMax
    // * 限购时用户最大购买数
    // * @param remain
    // * 商家剩余库存
    // * @param id
    // * 计数器的id
    // */
    // public void inItState(OnBuyNumChangedListener buyNumChangedListener,
    // int mTotalNum, int userMax, int remain, int id) {
    // this.goodsCounterId = id;
    // this.mTotalNum = mTotalNum;
    // this.userMax = userMax;
    // this.remain = remain;
    // this.mListener = buyNumChangedListener;
    // mInput.setText(String.valueOf(mTotalNum));// 当前一件
    // setListener();
    // setButtonEnabled();
    // }

    /**
     * @param activity 在java代码创建只初始化视图
     */
    public GoodsCounter(Activity activity) {
        super(activity);
    }

    /**
     * 设置减少按钮是否可以点击
     */
    private void setDecreaseButtonEnabled() {
        // TODO Auto-generated method stub
        if (decreaseButtonEnabled()) {
            mDecrease.setEnabled(true);
        } else {
            mDecrease.setEnabled(false);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    /**
     * 判断减少按钮是否可以点击
     *
     * @return
     */
    private boolean decreaseButtonEnabled() {
        // TODO Auto-generated method stub
        if (mTotalNum > 1) {
            return true;
        }
        return false;
    }

    //
    // @Override
    // public void onClick(View v) {
    // // TODO Auto-generated method stub
    // switch (v.getId()) {
    // case R.id.decrease_goods_num:
    // mTotalNum--;
    // isAdd = false;
    // mInput.setText(String.valueOf(mTotalNum));
    // setButtonEnabled();
    // break;
    //
    // case R.id.increase_goods_num:
    // mTotalNum++;
    // isAdd = true;
    // mInput.setText(String.valueOf(mTotalNum));
    // setButtonEnabled();
    // break;
    // }
    //
    // }

    public int getGoodsCounterId() {
        return goodsCounterId;
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        // mListener.onBuyNumChanged(mTotalNum, goodsCounterId, isAdd);
        if (mListener != null) {
            mListener.onBuyNumChanged(mInput.getText().toString().trim());
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub
        // mListener.onBuyNumChanged(mTotalNum);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

}

